﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;

public partial class Reports_BI_KPI_CashAtYear : System.Web.UI.Page
{

    #region Members

    StringBuilder sbTbl = new StringBuilder();
    string nl = @"
";
    DataTable tblData;
    int TblCurRow = 0;
    DateTime dt = DateTime.Now;
    int rowCount = 0;
    int rowAll = 0;
    int MonthStart = 1;
    int MonthStop = 1;
    int TurnoverFull = 0;
    int PlanFull = 0;

    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        Exception ret = null;

        oCaption.Text = "Динамика  ; Сегодня - " + String.Format("{0:00} / ", dt.Month) + dt.Year;

        ret = MakeData();
        if (ret != null)
        {
            oErr.Text = "<p>" + ret.Message + "</p>";
            return;
        }
        ret = MakeHTML_Table();
        if (ret != null)
        {
            oErr.Text = "<p>" + ret.Message + "</p>";
            return;
        }
        MakeHTML_Table();
    }

    private void MakeDataTable(ref DataTable tblSrc)
    {
        tblData = new DataTable("Chart");

        foreach (DataColumn c in tblSrc.Columns)
        {
            if (c.ColumnName == "Month")
                tblData.Columns.Add("Месяц", typeof(string));
            else
            {
                if (c.ColumnName == "2008")
                    continue;
                tblData.Columns.Add(c.ColumnName, typeof(Decimal));
            }
        }
    }

    private Exception MakeData()
    {
        Exception ret = null;
        dt = new DateTime(dt.Year, dt.Month, dt.Day);
        MonthStart = dt.Month;
        MonthStop = dt.Month;
        ret = FillDataTable();
        if (ret != null) return ret;
        return ret;
    }

    Exception FillDataTable()
    {

        Exception ret = null;
        rowAll = 0;
        rowCount = 0;
        for (int i = MonthStart; i <= MonthStop; i++)
        {
            TurnoverFull = 0;
            PlanFull = 0;
            DataTable tblSrc = new DataTable("tbl");
            Exception err = new Ko2.DB.BI.KPI.CashAtYear().Get(ref tblSrc, dt.Year, i);
            if (err != null)
            {
                ret = new Exception(err.Message, err);
                return ret;
            }
            MakeDataTable(ref tblSrc);
            foreach (DataRow r in tblSrc.Rows)
            {
                DataRow newRow = tblData.NewRow();

                foreach (DataColumn c in tblSrc.Columns)
                {
                    if (c.ColumnName == "Month")
                    {

                        if (r["Month"] != DBNull.Value && r["Month"].ToString() == "Все")
                            newRow["Месяц"] = "_Все";
                        else
                            newRow["Месяц"] = r[c.ColumnName];
                    }
                    else
                    {
                        if (c.ColumnName == "2008")
                            continue;
                        newRow[c.ColumnName] = r[c.ColumnName];
                    }
                }

                tblData.Rows.Add(newRow);
                rowAll++;
            }
        }

        return ret;
    }

    Exception MakeHTML_Table()
    {
        Exception ret = null;
        TblCurRow = -1;
        sbTbl.Append(
@"
<script language='javascript'>
//<![CDATA[
    function drawTable() {
        var data = new google.visualization.DataTable();" + nl);

        int AllYearColumsReal = 0;

        foreach (DataColumn c in tblData.Columns)
        {
            if (c.ColumnName == "Месяц")
                sbTbl.Append(@"data.addColumn('string', 'Месяц');" + nl);
            else
            {
                sbTbl.Append(@"data.addColumn('number', '" + c.ColumnName + @"');" + nl);
                AllYearColumsReal++;
            }
        }
        AllYearColumsReal--;

        sbTbl.Append(@"data.addRows(" + tblData.Rows.Count + @");" + nl);

        foreach (DataRow r in tblData.Rows)
            TblAddRow(r);

        sbTbl.Append(@"
        var table = new google.visualization.Table(document.getElementById('table_div'));
        table.draw(data, {showRowNumber: false, sort: 'disable'}); " + nl);

        sbTbl.Append(@"
        var data2 = new google.visualization.DataTable();
        data2.addColumn('string', 'Task');
        data2.addColumn('number', 'Hours per Day');
        data2.addRows(" + (AllYearColumsReal + 1) + @");");

        TblCurRow = -1;
        foreach (DataRow r in tblData.Rows)
        {
            if ((string)r["Месяц"] == "_Все")
            {
                foreach (DataColumn c in tblData.Columns)
                {
                    if (c.ColumnName != "Месяц")
                    {
                        TblCurRow++;
                        Int64 val = Convert.ToInt64(r[c.ColumnName]);
                        sbTbl.Append(@"
                            data2.setValue(" + TblCurRow + @", 0, '" + c.ColumnName + @"');
                            data2.setValue(" + TblCurRow + @", 1, " + val + @");" + nl);
                    }
                }
            }

        }

        sbTbl.Append(@"
        var oChartDiv2 = document.getElementById('table_div2');
        new google.visualization.PieChart(oChartDiv2).draw(data2, {title:""Оборот""});
");

        sbTbl.Append(@"
    }
//]]>
</script>");

        oBody.Text = sbTbl.ToString();
        return ret;
    }

    Exception TblAddRow(DataRow r)
    {
        Exception ret = null;
        TblCurRow++;
        int CurCol = -1;
        foreach (DataColumn c in tblData.Columns)
        {
            if (c.ColumnName == "2008")
                continue;

            CurCol++;
            if (c.ColumnName == "Месяц")
            {
                sbTbl.Append(@"
                    data.setCell(" + TblCurRow + @", 0, '" + r["Месяц"].ToString() + @"');" + nl);
            }
            else
            {
                Decimal val1 = 0;
                if (r[c.ColumnName] != DBNull.Value)
                    val1 = Convert.ToDecimal(r[c.ColumnName]);
                string str = val1.ToString("C"); str = str.Replace("р.", ""); // str.Replace(" ", "&nbsp;");
                if (str.Length > 3) str = str.Substring(0, str.Length - 3);
                sbTbl.Append(@"data.setCell(" + TblCurRow + @", " + CurCol + ", "
                    + Convert.ToInt64(val1) + @", '" + str + @"');" + nl);
            }
        }

        return ret;
    }

}